const express = require('express')
const urlmod = require('url')
const bodyParser = require('body-parser')
const config = require('config')
const session = require('express-session')
const RedisStore = require('connect-redis')(session)
const passport = require('./passport')
const cors = require('../cors')
const app = express()
const cookieParser = require('cookie-parser')
const headerSid = require('./headerSid')

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cookieParser())
// 添加session到header上以支持express-session
app.use(headerSid())
app.use(cors())
app.use(session({
  secret: config.session.api,
  store: new RedisStore({ host: '127.0.0.1', port: 6379, prefix: 'sess'}),
  cookie: {
    maxAge: 60 * 60 * 1000 * 24 * 365
  }
}))
app.use(passport.initialize())
app.use(passport.session())

app.use('/', require('./routes'))
// app.use('/v1', session({ secret: config.session.api }), require('./routes/op'))


module.exports = app